package service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.text.DecimalFormat;

import model.Department;
import model.User;
import model.UserRole;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.github.miemiedev.mybatis.paginator.domain.PageList;

import dao.DeptDao;
import dao.UserDao;
import dao.UserDeptDao;
import dao.UserRoleDao;

@Service
public class UserService {
	@Autowired
	private UserDao userDao;
	@Autowired
	private UserDeptDao userDeptDao;
	@Autowired
	private UserRoleDao userRoleDao;
	@Autowired
	private DeptDao deptDao;
	private static final String STR_FORMAT = "0000";

	/**
	 * 用户新增
	 * 
	 * @param user
	 * @return
	 */
	public void saveOrUpdate(User user, String flag) {
		// Date date = new Date();
		// Timestamp timestamp = new Timestamp(date.getTime());
		// UserDept userDept = new UserDept();
		if (user.getDept() != null && -1 == user.getDept()) {
			user.setDept(1);
		}
		user.setLasttime(new Date());
		if (user.getId() == null) {
			Department department = deptDao.getById(user.getDept());
			if (department.getLeaf().equals("true")) {
				Department pdept = deptDao.getById(department.getPid());
				String code = "";
				code += pdept.getCode(); // 区编号
				code += department.getCode(); // 社区编号
				DecimalFormat df = new DecimalFormat(STR_FORMAT);
				// String temp = df.format(user.getId()); // 本人ID 扩展为4位
				HashMap<String, Object> map = new HashMap<String, Object>();
				map.put("dept", user.getDept());
				Integer counts = userDao.queryCount(map) + 1;
				String temp = df.format(counts);
				code += temp;
				String _code = usercode(code);
				user.setCode(_code);
				userDao.save(user);
			}
		} else {
			// if (!StringUtils.isNotBlank(user.getCode())) {
			// Department department = deptDao.getById(user.getDept());
			// if (department.getLeaf().equals("true")) {
			// Department pdept = deptDao.getById(department.getPid());
			// String code = "";
			// code += pdept.getCode();
			// code += department.getCode();
			// DecimalFormat df = new DecimalFormat(STR_FORMAT);
			// HashMap<String, Object> map = new HashMap<String, Object>();
			// map.put("dept", user.getDept());
			// Integer counts = userDao.queryCount(map);
			// String temp = df.format(counts);
			// code += temp;
			// String _code = usercode(code);
			// user.setCode(_code);
			// }
			// }
			userDao.update(user);
		}
		if (!StringUtils.isNotBlank(flag)) {
			saveUserRole(user);
		}
	};

	public String usercode(String code) {
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("code", code);
		Integer counts = userDao.queryCount(map);
		if (counts != 0) {
			Integer temp = Integer.parseInt(code) + 1;
			usercode(temp.toString());
		}
		return code;
	}

	/**
	 * 返回分页 结果
	 * 
	 * @param page
	 * @param pageSize
	 * @return
	 */
	public PageList<HashMap<String, Object>> queryUserPage(int page, int limit,
			HashMap<String, Object> map) {
		PageBounds pageBounds = new PageBounds(page, limit);
		List<HashMap<String, Object>> list = userDao.queryUserPage(pageBounds,
				map);
		// List<User> listUser= list.get
		// 获得结果集条总数
		PageList<HashMap<String, Object>> pageList = (PageList<HashMap<String, Object>>) list;
		return pageList;
	};

	/**
	 * 根据用户id拿到角色
	 * 
	 * @param userid
	 * @return
	 */
	public List<Integer> queryRoleIds(Integer userid) {
		List<Integer> list = new ArrayList<Integer>();
		List<UserRole> userRoleList = userRoleDao.getUserRoles(userid);
		for (UserRole ur : userRoleList) {
			list.add(ur.getRoleid());
		}
		return list;
	}

	public User getById(Integer id) {
		return userDao.getById(id);
	}

	public void delete(String ids) {
		String[] idStrs = ids.split(",");
		for (String id : idStrs) {
			Integer _id = Integer.parseInt(id);
			// 删除用户
			userDao.delete(_id);
		}
	}

	/**
	 * 保存 用户的角色
	 * 
	 * @param user
	 */
	public void saveUserRole(User user) {
		userRoleDao.delete(user.getId());
		if (user.getRoles().isEmpty()) {
			return;
		}
		String[] ids = user.getRoles().split(",");
		for (String item : ids) {
			UserRole userRole = new UserRole();
			Integer temp = Integer.parseInt(item);
			userRole.setRoleid(temp);
			userRole.setUserid(user.getId());
			userRoleDao.save(userRole);
		}
	}

	public User load(User user) {
		return userDao.load(user);
	}

	public HashMap<String, Object> getCurUser(Integer id) {
		return userDao.getCurUser(id);
	}

	/**
	 * 用户名唯一
	 * 
	 * @author yakun0622
	 * @param map
	 * @return
	 */
	public User uniqueName(HashMap<String, Object> map) {
		return userDao.uniqueName(map);
	}

}
